﻿using System;
using System.Linq;
using System.Web.Mvc;
using Willow.Model;
using Willow.MVC.Result;
using Willow.MVC.Search;
using Willow.IService.Auth;
using System.Collections.Generic;
using Willow.Extensions;
using Willow.MVC.Result;
using Newtonsoft.Json;
namespace Willow.WebApplication.Areas.Auth.Controllers
{
    public class UserController : Controller
    {

        #region 属性

        public IUserService UserService { get; set; }

        public IUserRoleService UserRoleService { get; set; }

        #endregion

        #region 页面

        /// <summary>
        /// 用户管理页
        /// </summary>
        /// <returns></returns>
        public ViewResult Management()
        {
            return View();
        }

        #endregion

        #region 操作

        /// <summary>
        /// 导出Excel文档
        /// </summary>
        /// <param name="sort">The sort.</param>
        /// <param name="order">The order.</param>
        /// <param name="orgId">The org identifier.</param>
        /// <param name="query">The query.</param>
        /// <returns></returns>
        public ActionResult ExportExcel(string sort, string order, string orgId, QueryModel query)
        {
            int total = 0;
            var filter = UserService.Query(p => p.IsDelete == false);
            if (!string.IsNullOrWhiteSpace(orgId))
            {
                filter = filter.Where(p => p.Auth_Organization.FullParentId.Contains(orgId));
            }
            var lstData = filter
                .OrderBy(new PropertySortCondition(sort, order))
                .Where(query)
                .Select(p => new
                {
                    p.UserName,
                    p.FullName,
                    p.Birthday,
                    OrgName = p.Auth_Organization.Name
                }).ToList()
                .Select(p => new
                {
                    登陆账户 = p.UserName,
                    姓名 = p.FullName,
                    出生日期 = p.Birthday.ToShortDateString(),
                    组织 = p.OrgName
                });
            return this.ExportExcel(lstData, "用户列表");
        }

        /// <summary>
        /// 导出Word文档
        /// </summary>
        /// <param name="userId">The user identifier.</param>
        /// <returns></returns>
        public ActionResult ExportWord(Guid userId)
        {
            var user = UserService.Find(userId);
            var templatePath = Server.MapPath(@"~/ExportTemplate/Word/用户信息模板.tpl");
            var placeholders = new Dictionary<string, string>();
            placeholders.Add("用户账号", user.UserName);
            placeholders.Add("密码", user.Password);
            placeholders.Add("出生日期", user.Birthday.ToShortDateString());
            placeholders.Add("组织", user.Auth_Organization.Name);
            return this.ExportWord("用户个人信息", templatePath, placeholders);
        }

        /// <summary>
        /// 获取用户相关角色
        /// </summary>
        /// <param name="userId">The user identifier.</param>
        /// <returns></returns>
        public JsonResult GetRelatedRole(Guid userId)
        {
            var data = UserRoleService.Filter(p => p.UserId == userId)
                .Select(p => p.RoleId);
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取用户分页后信息
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="rows">The rows.</param>
        /// <param name="orgId">The org identifier.</param>
        /// <param name="query">The query.</param>
        /// <returns></returns>
        public JsonResult GetPagedList(int page, int rows, string sort, string order, string orgId, QueryModel query)
        {
            int total = 0;
            var filter = UserService.Query(p => p.IsDelete == false);
            if (!string.IsNullOrWhiteSpace(orgId))
            {
                filter = filter.Where(p => p.Auth_Organization.FullParentId.Contains(orgId));
            }
            var lstData = filter
                .OrderBy(new PropertySortCondition(sort, order))
                .Where(query, page, rows, out total)
                .Select(p => new
                {
                    p.UserName,
                    p.Password,
                    p.FullName,
                    p.Gender,
                    p.Birthday,
                    OrganizationId = p.OrganizationId,
                    OrganizationName = p.Auth_Organization.Name,
                    Id = p.Id
                }).ToList();

            return this.JsonNet(new { rows = lstData, total });
        }

        /// <summary>
        /// 保存用户
        /// </summary>
        /// <param name="user">The user.</param>
        /// <returns></returns>
        public JsonResult Save(Auth_User user)
        {
            return this.StandardJson(() =>
            {
                user.IsDelete = false;
                UserService.Save(user);
            });
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="ids">The ids.</param>
        /// <returns></returns>
        public JsonResult Delete(Guid[] ids)
        {
            return this.StandardJson(() =>
            {
                UserService.BatchLogicalDelete(ids);
            });
        }

        #endregion

    }
}